﻿using Entity.BaseEntitys;
using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Sharding.PaginationConfigurations;
using ShardingCore.VirtualRoutes.Months;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Entity.BaseSplit
{

    /// <summary>
    /// Table in chronological default current year January points constraint from the create the entity
    /// </summary>
    public class BaseSplitTimeTableRoute<T,Key> : AbstractSimpleShardingMonthKeyDateTimeVirtualTableRoute<T> where T :  CreateEntity<Key>
    {
        
        public override DateTime GetBeginTime() => new DateTime(DateTime.Now.Year, 1, 1);

        public override bool? EnableRouteParseCompileCache => true;
        public override void Configure(EntityMetadataTableBuilder<T> builder)
         => builder.ShardingProperty(o => o.CreateTime);

        public override IPaginationConfiguration<T> CreatePaginationConfiguration()
            => new SplitTablePaginationConfiguration<T, Key>();


        public override bool AutoCreateTableByTime() => true;
    }

    public class SplitTablePaginationConfiguration<T,Key> : IPaginationConfiguration<T> where T : CreateEntity<Key>
    {
        public void Configure(PaginationBuilder<T> builder)
        {
            builder.PaginationSequence(o => o.CreateTime)
                .UseRouteComparer(Comparer<string>.Default)
                .UseQueryMatch(PaginationMatchEnum.Owner | PaginationMatchEnum.Named | PaginationMatchEnum.PrimaryMatch).UseAppendIfOrderNone(10);

            builder.ConfigReverseShardingPage(1d, 10000L);
        }
    }
}
